import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class AnimatedEllipsisText extends StatefulWidget {
  const AnimatedEllipsisText(
    this.text, {
    super.key,
    this.style,
  });

  final String text;

  final TextStyle? style;

  @override
  State<StatefulWidget> createState() => _AnimatedEllipsisText();
}

class _AnimatedEllipsisText extends State<AnimatedEllipsisText>
    with SingleTickerProviderStateMixin {
  late final Timer timer;
  String ellipsis = ".";

  @override
  void initState() {
    super.initState();
    timer = Timer.periodic(const Duration(milliseconds: 500), (_) {
      setState(() {
        ellipsis += ".";
        if (ellipsis.length > 6) {
          ellipsis = ".";
        }
        /* _numberOfEllipsis = _numberOfEllipsis % 4; */
      });
    });
  }

  @override
  void dispose() {
    if (timer.isActive) timer.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            alignment: Alignment.center,
            width: 120.h,
            child: Text(
              widget.text,
              style: widget.style,
            ),
          ),
          Container(
            alignment: Alignment.center,
            width: 120.h,
            child: Text(
              ellipsis,
              style: widget.style,
            ),
          ),
        ],
      ),
    );
  }
}

/* extension on int {
  String toEllipsis() => '.' * this;
}
 */